//package com.pug.service.userorder;
//
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//import com.pug.anno.log.PugMessage;
//import com.pug.bo.UserOrderBo;
//import com.pug.dto.UserOrderDto;
//import com.pug.entity.Hotel;
//import com.pug.entity.HotelType;
//import com.pug.entity.KssUser;
//import com.pug.entity.UserOrder;
//import com.pug.event.MongoEvent;
//import com.pug.mapper.userorder.UserOrderMapper;
//import com.pug.mq.order.UserOrderProducer;
//import com.pug.service.BaseService;
//import com.pug.service.order.IUserOrderService;
//import com.pug.threadlocal.UserThrealLocal;
//import com.pug.utils.fn.asserts.Vsserts;
//import com.pug.vo.OrderPayVo;
//import com.pug.vo.UserOrderVo;
//import com.pug.vo.UserVo;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.util.List;
//import java.util.Map;
//
///**
// * @author 飞哥
// * @Title: 学相伴出品
// * @Description: 飞哥B站地址：https://space.bilibili.com/490711252
// * 记得关注和三连哦！
// * @Description: 我们有一个学习网站：https://www.kuangstudy.com
// * @date 2022/4/2$ 21:01$
// */
//@Service
//@Slf4j
//public class UserOrderServiceImpl extends ServiceImpl<UserOrderMapper, UserOrder> implements IUserOrderService, BaseService {
//
//
//    @Autowired
//    private UserOrderProducer userOrderProducer;
//
//
//    /**
//     * 查询用户订单状态信息
//     * @return
//     */
//    @Override
//    public List<Map<String,Object>> selectOrderStatusInfo(){
//        KssUser user = UserThrealLocal.get();
//        return this.baseMapper.selectOrderStatusInfo(user.getId());
//    }
//
//
//    @Override
//    public Page<UserOrder> findUserOrders(UserOrderVo userOrderVo) {
//        KssUser user = UserThrealLocal.get();
//        // 1: 设置查询的条件
//        Page<UserOrder> page = new Page<>(userOrderVo.getPageNo(), userOrderVo.getPageSize());
//        // 2: 设置条件
//        LambdaQueryWrapper<UserOrder> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(Vsserts.isNotNull(userOrderVo.getPaystatus()),
//                UserOrder::getStatus, userOrderVo.getPaystatus());
//        // 3: 查询某个用户的订单信息
//        lambdaQueryWrapper.eq(UserOrder::getUserId, user.getId());
//        // 4:可以根据订单编号查询或者标题查询
//        lambdaQueryWrapper.like(Vsserts.isNotNull(userOrderVo.getKeyword()),
//                UserOrder::getHotelTitle, userOrderVo.getKeyword());
//        // 5: 考虑排序
//        lambdaQueryWrapper.orderByDesc(UserOrder::getCreateTime);
//        return this.page(page, lambdaQueryWrapper);
//    }
//
//
//    @Override
//    @PugMessage(eventClasses = {MongoEvent.class})
//    //@PugMQMessage(routingkey = "es.mongo.ordermessage")
//    @Transactional
//    public UserOrder createUserOrder(Hotel hotel,
//                                     HotelType hotelType,
//                                     KssUser user, OrderPayVo orderPayVo, String orderNumber) {
//
//        UserOrder userOrder = null;
//        // 如果当前已经存在一笔订单，就不做任何处理，直接继续支付即可
//        if (Vsserts.isNotEmpty(orderPayVo.getOrderno())) {
//            userOrder = this.getById(Long.parseLong(orderPayVo.getOrderno()));
//        }
//
//        // 如果没有，创建一笔订单
//        if (Vsserts.isNull(userOrder)) {
//            // 创建一个订单
//            userOrder = new UserOrder();
//            userOrder.setOrderno(orderNumber + "");
//            userOrder.setUserId(user.getId());
//            userOrder.setAvatar(user.getAvatar());
//            userOrder.setNickname(user.getNickname());
//            userOrder.setStatus(0);//未支付状态
//            userOrder.setPaymoney(hotelType.getRealprice().toString());
//            userOrder.setHotelId(hotel.getId());
//            userOrder.setHotelImg(hotel.getImg());
//            userOrder.setHotelAddress(hotel.getAddress());
//            userOrder.setHotelTel(hotel.getPhone());
//            userOrder.setHotelLgt(hotel.getLgt());
//            userOrder.setHotelLan(hotel.getLan());
//            userOrder.setHotelTitle(hotel.getTitle());
//            userOrder.setHotelDesc(hotel.getDescription());
//            userOrder.setHotelDetailId(hotelType.getId());
//            userOrder.setHotelDetailTitle(hotelType.getTitle());
//            userOrder.setHotelDetailImg(hotelType.getImg());
//            userOrder.setTradeno("");
//            userOrder.setOpenid(orderPayVo.getOpenid());
//            //userOrder.setIp(IpUtils.getIpAddr(resultMap));
//            userOrder.setIpaddress("");
//            userOrder.setPaytype(1);
//            this.saveOrUpdate(userOrder);
//            // MQ mongodb消息
//            // MQ 30min超时
//        }
//        //1: 订单消息 --- 使用AOP去完成了
//
//        //2: MQ延时订单处理 + AOP
//        UserOrderDto userOrderDto = this.tranferBo(userOrder, UserOrderDto.class);
//        userOrderProducer.pusherOrder(userOrderDto);
//
//        return userOrder;
//
//    }
//
//    /**
//     * 用户下单处理
//     *
//     * @param userVo
//     * @return
//     */
//    @Override
//    @Transactional
//    public UserOrderBo createUserOrder(UserVo userVo) {
//        // 1: 获取当前下单的用户信息
//        KssUser kssUser = UserThrealLocal.get();
//        // 2: 创建用户订单实例
//        UserOrder userOrder = new UserOrder();
//        userOrder.setOrderno(userVo.getOrderNo());
//        userOrder.setStatus(0);// 代表未支付
//
//        userOrder.setUserId(kssUser.getId());
//        userOrder.setNickname(kssUser.getNickname());
//        userOrder.setAvatar(kssUser.getAvatar());
//        log.info("用户开始下单，订单信息是： 【{}】", userOrder);
//
//        // 3：执行数据的订单保存功能
//        boolean b = this.saveOrUpdate(userOrder);
//        if (b) {
//            // 4:获取到订单信息开始进行异步发送处理
//            UserOrderBo userOrderBo = tranferBo(userOrder, UserOrderBo.class);
//            // 5: 对内进行发送异步处理
//            UserOrderDto userOrderDto = tranferBo(userOrder, UserOrderDto.class);
//            // 6 : 使用mq进行异步处理
//            userOrderProducer.pusherOrder(userOrderDto);
//            return userOrderBo;
//        } else {
//            return null;
//        }
//    }
//
//
//    /**
//     * sql语句写法：
//     * UPDATE kss_user_order SET status = -1 WHERE id in(
//     * SELECT c.id from (
//     * SELECT id,TIMESTAMPDIFF(MINUTE,create_time,NOW()) as mins FROM kss_user_order WHERE status = 0
//     * ) c WHERE c.mins > 30
//     * )
//     */
//    @Override
//    public void findOrderNoPay() {
//
//        //这样写也行。
//        this.baseMapper.updateNoPayOrderStatus();
//
////        LambdaQueryWrapper<UserOrder> lambdaQueryWrapper = new LambdaQueryWrapper<>();
////        lambdaQueryWrapper.eq(UserOrder::getStatus,0);
////        List<UserOrder> userOrders = this.list(lambdaQueryWrapper);
////        if(!CollectionUtils.isEmpty(userOrders)) {
////            List<UserOrder> userOrdersFilter = userOrders.parallelStream().filter(userOrder -> {
////                int diffminutes = TmDateUtil.diffminutes(userOrder.getCreateTime(),new Date());
////                return diffminutes >= 20;
////            }).map(userOrder -> {
////                UserOrder userOrder1 = new UserOrder();
////                userOrder1.setId(userOrder.getId());
////                userOrder1.setStatus(-1);
////                return userOrder1;
////            }).collect(Collectors.toList());
//
////            this.updateBatchById(userOrdersFilter, 100);
////        }
//    }
//
//}
